package com.feishi.core.spring.demo;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: caixq
 * Date: 2020-11-23
 * Time: 上午10:42
 */
public class A {
    public static void main(String[] args) throws InterruptedException {
//        testLock();
//        testCyclicBarrier();
//        testPhaser();
//        testSemaphore();
        testExchanger();



    }

    private static void testExchanger() {
        Exchanger<String> exchanger=new Exchanger<>();
        new Thread(()->{
            try {
                String a="a";
                System.out.println("a="+exchanger.exchange(a));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(()->{

            try {
                String b="b";
                Thread.sleep(10000);
                System.out.println("b="+exchanger.exchange(b));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

    private static void testSemaphore() {
        Semaphore semaphore=new Semaphore(3,true);//许可数量，用于限流
        long time=System.nanoTime();
        for (int i = 0; i < 10; i++) {
            int finalI = i;
            new Thread(()->{
                try {
                    semaphore.acquire();
                    Thread.sleep(100);
                    System.out.println(">>>>>>>>>>>>>>>>>>"+finalI);
                    System.out.println(System.nanoTime()-time);
                    semaphore.release();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }

    private static void testPhaser() {
        Phaser phaser=new Phaser();
        phaser.bulkRegister(30);//每满30个放开一次围栏

        for (int i = 0; i < 30; i++) {
            int finalI = i;
            new Thread(()->{
//                phaser.arriveAndDeregister();
                phaser.arriveAndAwaitAdvance();
                System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+finalI);
                phaser.arriveAndAwaitAdvance();
                System.out.println(finalI);
                if(finalI%2==0){
                    phaser.arriveAndDeregister();
                    return;//撤销一个上限
                }
                phaser.arriveAndAwaitAdvance();
                System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" + finalI);
                phaser.arriveAndDeregister();

//                System.out.println(phaser.arrive());
            }).start();
        }
    }

    private static void testCyclicBarrier() {
        AtomicInteger a= new AtomicInteger();
        CyclicBarrier cyclicBarrier=new CyclicBarrier(20, () -> {
            System.out.println(a.getAndIncrement());
//            System.out.println(1/0);

        });
        for (int i = 0; i < 100; i++) {
            int finalI = i;
            new Thread(()->{
                try {
                    cyclicBarrier.await();
                    System.out.println("i:"+ finalI);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (BrokenBarrierException e) {
                    e.printStackTrace();//回调异常会触发此异常
                }
            }).start();
        }
    }

    private static void testLock() throws InterruptedException {

        long time=System.nanoTime();
        TimeUnit.SECONDS.sleep(10);
        ReentrantLock lock=new ReentrantLock();
        lock=new ReentrantLock(true);//公平锁，线程队列等待，(非公平锁直接竞争锁)
//        lock.lock();
//        lock.lock();
//        boolean lo = lock.tryLock(10, TimeUnit.SECONDS);
//        lock.lockInterruptibly();//
//        lock.unlock();
        System.out.println(System.nanoTime()-time); ;

        ReentrantLock finalLock = lock;
        for (int i = 0; i < 1000; i++) {
            Thread.sleep(1);
            int finalI = i;
            new Thread(()->{
                finalLock.lock();
                System.out.println(finalI);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                finalLock.unlock();
            }).start();
        }
    }
}
